import Vue from 'vue'
// #ifdef H5
var wx = require('@/common/jweixin-module@1.6.0_lib_index.js');
//#endif
//var QQMapWX = require('@/components/qqmap-wx-jssdk.min.js');

var qqmapsdk;
var wxconf;
export default {
	//微信隐藏
	wxapi:function(appid,domain,wxid,curl,apistr){
		if(!isWechat){
			return;
		}
		let apis=apistr.split(",");
		if(apis.length==0){
			console.log("apis is empty")
			return
		}
		curl = curl ? curl : window.location.href;
		curl=curl.split('#')[0];
		curl=escape(curl);
		uni.request({
			url:domain+"/wxmp/"+appid+"/sharejsconf?wxid="+wxid+"&url="+curl,
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'Safescode':uni.getStorageSync('safescode')
			},
			success: (res) => {
				wx.config({
				    debug: false,
				    appId: res.data.appid,
				    timestamp: res.data.timestamp,
				    nonceStr: res.data.noncestr,
				    signature: res.data.signstr,
				    jsApiList: apis
				});
				wx.ready(function() {
					apis.forEach(function(e){
						if(e=="hideAllNonBaseMenuItem"){
							wx.hideAllNonBaseMenuItem();
						}
					})
				})
			}
		})
	},
	//微信分享
	wxshareconfInit:function(appid,domain,wxid,curl,title,desc,img,clickurl){
		if(!isWechat){
			return;
		}
		if(wxid==undefined){
			return;
		}
		curl = curl ? curl : window.location.href;
		let apis=[
			    'checkJsApi',
				'onMenuShareTimeline',
				'onMenuShareAppMessage',
				'chooseWXPay',
				'scanQRCode'
				];
		if(Vue.prototype.wx_lbs=="1"){
			apis=[
			    'checkJsApi',
				'onMenuShareTimeline',
				'onMenuShareAppMessage',
				'getLocation',
				'chooseWXPay',
				'scanQRCode'
				];
			qqmapsdk = new QQMapWX({
				key: 'BGVBZ-DAG3K-5SCJC-ALFHM-37M35-RWFOK'
			});
		}
		curl=curl.split('#')[0];
		curl=escape(curl);
		
		uni.request({
			url:domain+"/wxmp/"+appid+"/sharejsconf?wxid="+wxid+"&url="+curl,
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'Safescode':uni.getStorageSync('safescode')
			},
			success: (res) => {
				wxconf=res.data;
				wx.config({
				    debug: false,
				    appId: res.data.appid,
				    timestamp: res.data.timestamp,
				    nonceStr: res.data.noncestr,
				    signature: res.data.signstr,
				    jsApiList: apis
				});
				wx.ready(function() {
				    var shareData = {
				        title: title,
				        desc: desc,
				        link: clickurl,
				        imgUrl: img,
				        success: function(e) {
				            // 分享后的一些操作,比如分享统计等等
				        },
				        cancel: function(e) {}
				    };
					console.log("shareData:",shareData);
				    //分享给朋友接口  
				    wx.onMenuShareAppMessage(shareData);
				    //分享到朋友圈接口  
				    wx.onMenuShareTimeline(shareData);
					
					//获取位置
					console.log("wx_lbs",Vue.prototype.$islbs)
	// 				if(getApp().globalData.$islbs=="1"){
	
	// 					wx.getLocation({
	// 						type:'wgs84',
	// 						success: function (res_zb) {
	// 							//alert("res——zb"+" :"+res_zb+"zz "+res_zb.latitude);
	// 							// alert('res_zb.latitude',res_zb.latitude)
	// 							try {
	// 							    uni.setStorageSync('zb', res_zb);
	// 							} catch (e) {
	// 							    console.log("保存坐标失败",e)
	// 								//alert("保存坐标失败",e)
	// 							}
								
	// 						},
	// 						fail:function(res){
	// 							//alert("getlocation",res)
	// 							console.log("getlocation",res);
	// 						}
	// 					})
	// 				}
					
					
				});
			} 
		})		
		return "";
	},
	funcQQMapAddressXcx:function(latitude,longitude){
		qqmapsdk = new QQMapWX({
			key: 'BGVBZ-DAG3K-5SCJC-ALFHM-37M35-RWFOK'
		});
		qqmapsdk.reverseGeocoder({
			location:{
				latitude:latitude,
				longitude:longitude
			},
			success: (rst) => {
				try {
					alert("addr:"+rst.result.address)
				    uni.setStorageSync('_wxaddress', rst.result.address);
				} catch (e) {
				    console.log("保存坐标失败",e)
					alert("保存坐标失败"+e)
				}
			},
			fail:(error)=>{
				var temp="";
				for(var i in error){
					temp+=i+":"+error[i]+"\n";
				}
				//alert("address fail"+temp);
				console.log("address fail",error);
			}
		})
	},
	funcQQMapAddressH5:function(latitude,longitude){
		// latitude=38.015097;
		// longitude=114.511933;
		// qqmapsdk = new QQMapWX({
		// 	key: 'BGVBZ-DAG3K-5SCJC-ALFHM-37M35-RWFOK'
		// });
		var rval="";
		uni.request({
			url:getApp().globalData.$websiteUrl+"/app2/qqmaplocationh5",
			data:{
				location:latitude+","+longitude,
				code:getApp().globalData.$appCode
			},
			//url:"/txmap/api/gljs?v=1.exp&key=BGVBZ-DAG3K-5SCJC-ALFHM-37M35-RWFOK",
			//url:"/txmap/ws/geocoder/v1/?location="+latitude+","+longitude+"&key=BGVBZ-DAG3K-5SCJC-ALFHM-37M35-RWFOK",
			//url:"https://apis.map.qq.com/ws/geocoder/v1?location="+latitude+","+longitude+"&key=BGVBZ-DAG3K-5SCJC-ALFHM-37M35-RWFOK",
			success(rst) {
				// console.log("rst",rst)
				// rval=rst.msg;
				// Vue.prototype.$wx_location=rst.msg;
				uni.setStorage({
				    key: '_wxaddress',
				    data: rst.data.msg,
				    success: function () {
				        console.log('success',rst.data.msg);
				    }
				});
				
			},
			fail(error) { 
				var temp="";
				for(var i in error){
					temp+=i+":"+error[i]+"\n";
				}
				//alert("address fail"+temp);
				console.log("address fail",error);
			}
		})
	},
	funcTest:function(latitude,longitude){
		latitude=38.015097;
		longitude=114.511933
		qqmapsdk = new QQMapWX({
			key: 'BGVBZ-DAG3K-5SCJC-ALFHM-37M35-RWFOK'
		});
		qqmapsdk.reverseGeocoder({
			location:{
				latitude:latitude,
				longitude:longitude
			},
			success: (rst) => {
				try {
					alert("addr:"+rst.result.address)
				    uni.setStorageSync('address', rst.result.address);
				} catch (e) {
				    console.log("保存坐标失败",e)
					alert("保存坐标失败"+e)
				}
			},
			fail:(error)=>{
				var temp="";
				for(var i in error){
					temp+=i+":"+error[i]+"\n";
				}
				alert("address fail"+temp);
				console.log("address fail",error);
			}
		})
	},
    funcUniGetLocation:function(){
		// uni.getLocation({
		// 	success: (r) => {
		// 		//alert('res_zb.latitude',r.latitude)
		// 		try {
		// 		    uni.setStorageSync('zb', r.latitude);
		// 		} catch (e) {
		// 		    console.log("保存坐标失败",e)
		// 			//alert("保存坐标失败",e)
		// 		}
		// 	},
		// 	fail: (e) => {
		// 		//alert("location fail",e)
		// 	}
		// })
	},
	//微信扫一扫
	funcScan:function(js,row){
		wx.scanQRCode({
			needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
			scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
			success: function (res) {
				let str = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
				console.log(str)
				eval(js);
			}
		})
	},
	//微信扫一扫 结果交给微信处理
	funcScan2:function(){
		wx.scanQRCode({
			needResult: 0, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
			scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
			success: function (res) {
			}
		})
	},
}
//判断是否在微信环境
function isWechat(){
	var ua = window.navigator.userAgent.toLowerCase();
	if (ua.match(/micromessenger/i) == 'micromessenger') {
	   // console.log('是微信客户端')
	   return true;
	} else {
	   // console.log('不是微信客户端')
	   return false;
	}
}

function qqMapLocation(latitude,longitude){
	qqmapsdk = new QQMapWX({
		key: 'BGVBZ-DAG3K-5SCJC-ALFHM-37M35-RWFOK'
	});
	qqmapsdk.reverseGeocoder({
		location:{
			latitude:l,
			longitude:r
		},
		success: (rst) => {
			//alert("address"+rst.result.address);
			console.log("address",rst,rst.result.address);
			// //alert(rst.result.address);
			// Vue.prototype.$location =rst.result.address;
			// try {
			//     uni.setStorageSync('location', rst.result.address);
			// } catch (e) {
			//     console.log("保存位置失败",e)
			// }
			// return rst.result.address;
		},
		fail:(error)=>{
			//alert("address fail",error);
			console.log("address fail",error);
		}
		// success:function(rst){
		// 	console.log("address",rst,rst.result.address);
		// 	this.wx_location=rst.result.address;
		// },
		//  fail: function (rst) {
		// 		 console.log('fail',rst);
		//  },
	})
}